<template>
    <Card>
        <div>
            <Affix :offset-top="120">
                <div class="contract-detial-btns" v-if="isApprove">
                    <Button type="primary" @click="back">返回</Button>
                    <Button type="primary" @click="approve('pass')" v-if="!this.readOnly">通过</Button>
                    <Button type="primary" @click="approve('return')" v-if="!this.readOnly">退件</Button>
                    <Button type="primary" @click="approve('reject')" v-if="!this.readOnly">拒绝</Button>
                </div>
                <div class="contract-detial-btns" v-else>
                    <Button type="primary" @click="back">返回</Button>
                    <Button type="primary" @click="save" v-if="!this.readOnly">保存</Button>
                    <Button type="primary" @click="submit" v-if="!this.readOnly">提交</Button>
                </div>
            </Affix>
        </div>
        <Collapse v-model="openPanel">
            <Panel name="1" v-if="isApprove||this.pageParams.processStatus">
                日志
                <div class="content" slot="content">
                    <approveHistory :Obj="Obj"/>
                </div>
            </Panel>
            <Panel name="2">
                基本信息
                <div class="content" slot="content">
                    <BaseInfo ref="baseInfo" :formData="pageInfo"/>
                </div>
            </Panel>
            <Panel name="3">
                合同信息
                <div class="content" slot="content">
                    <contractInfo :Obj="Obj"
                                  ref="contractInfo"
                                  :readOnly="readOnly"
                    />
                </div>
            </Panel>
            <!--            <Panel name="4">-->
            <!--                金融产品-->
            <!--                <div class="content" slot="content">-->
            <!--                    <FinanceInfo :Obj="Obj"/>-->
            <!--                </div>-->
            <!--            </Panel>-->
            <Panel name="7" v-if="affiliatedType">
                <!-- <Alert>挂靠信息</Alert> -->
                挂靠信息
                <div class="content" slot="content">
                    <CustAnchorInfo ref="custAnchorInfo" @affType="affType" :Obj="Obj"/>
                </div>
            </Panel>
            <Panel name="5">
                业务办理
                <div class="content" slot="content">
                    <Business
                        ref="business"
                        :Obj="Obj"
                        :isSP="this.isSP"
                        :pageParams="pageInfo"
                        :dataDict="dataDict"
                        :readOnly="this.readOnly"
                        @saveDataFn="saveDataFn"
                        @submitDataFn="submitDataFn"
                        @childBusinessForm="childBusinessForm"
                        @closeSpin="closeSpin"
                    />
                </div>
            </Panel>
            <Panel name="6" v-if="isApprove&&!readOnly">
                当前用户: {{userInfo ? userInfo.userRealName :''}}
                <div class="content" slot="content">
                    <ContractCurrentUser ref="contractCurrentUser" :Obj="{type:'reprintContract'}"
                                         @childUserForm="childUserForm"
                                         @closeSpin="closeSpin"/>
                </div>
            </Panel>
        </Collapse>
    </Card>
</template>
<script>

    import {
        saveContractReissue,
        submitContractReissue,
        saveProposalApplicationInfo
    } from "_p/basic/api/contract/contract-reissue/index";
    import {businessSource, channelType, Finance, FinanceType} from "_p/basic/api/contract/channel-type/channel-type";
    // 日志
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    // 金融产品
    import FinanceInfo from "_p/basic/pages/contract/common/finance-info/finance-info";
    // 业务办理
    import Business from "_p/basic/pages/contract/contract-reissue/contract-reissue-detial/Business";
    // 合同信息
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    // 基本信息
    import BaseInfo from "_p/basic/pages/contract/common/base-info";
    // 当前用户
    import ContractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    // 挂靠信息
    import CustAnchorInfo from "_p/basic/pages/contract/common/cust-anchor/custAnchorInfo";


    export default {
        name: "ContractReissueDetial",
        components: {
            approveHistory,
            FinanceInfo,
            contractInfo,
            BaseInfo,
            ContractCurrentUser,
            Business,
            CustAnchorInfo,
        },
        props: {
            Obj: {
                type: Object,
                required: true
            },
            spinShow: {
                type: Boolean,
                required: true
            },
            // 是否是供应商
            isSP: {
                type: Boolean,
                required: true
            },
            //是否是审批页面
            isApprove: {
                type: Boolean,
                required: true
            },
            pageParams: {
                type: Object,
                required: true
            },
            //合同号
            contractNo: {
                type: String,
                required: true
            },
            dataDict: {
                type: Object
            },
            userInfo: {
                type: Object,
                required: true
            },
            roleType: {
                type: Array,
                required: true
            }
        },
        data() {
            return {
                contractForm: {
                    selecteds: [],
                    channelFullName:null,
                    channelBelong:null,

                },
                openPanel: [2, 3, 4, 5, 6, 7],
                type: null,
                readOnly: null,
                approveType:'',
                caseNo: null,
                loading: true,
                pageInfo: this.pageParams,
                affiliatedType: true,
                // isSP: null,
                id: null,
            };

        },

        mounted() {
            console.group("index页面初始数据：");
            console.log("userInfo:", this.userInfo);
            console.log("pageParams:", this.pageParams);
            console.log("roleType:", this.roleType);
            console.log("isSP:", this.isSP);
            console.log("readOnly:", this.readOnly);
            console.log("isApprove:", this.isApprove);
            console.log("contractNo:", this.contractNo);
            this.init();

        },
        methods: {
            init() {
                if (this.$refs.baseInfo) {
                    this.$refs.baseInfo.formData = Object.assign(
                        {},
                        this.$refs.baseInfo.formData,
                        this.pageInfo
                    );

                }
                this.isSP = this.isSP
                console.log("this.pageInfo.type", this.pageInfo.type)
                if (this.pageInfo.type == 'read') {
                    this.readOnly = true

                }
                console.log(" this.readOnly", this.readOnly)
                if (this.isSP) {
                    this.$Message.success("合同下载有效期15天,请在15天内打印!");
                }

            },
            formData() {
                this.contractForm = this.$refs.business.formData;
                this.contractForm.selecteds = this.$refs.business.selecteds
                this.contractForm.channelFullName = this.$refs.contractInfo.formData.channelFullName
                this.contractForm.channelBelong = this.$refs.contractInfo.formData.channelBelong;
                this.contractForm.businessSource = businessSource(this.roleType);
                console.log("this.contractForm", this.contractForm)
            },
            save() {
                if (this.$refs.business) {

                    this.showSpin();
                    this.$refs.business.formValidate("saveDataFn");
                }
            },
            affType(type) {

                this.affiliatedType = type;
                console.log("---this.affiliatedType-----=" + this.affiliatedType);

            },

            submit() {
                this.$Modal.confirm({
                    title: "确认提交？",
                    onOk: () => {
                        if (this.$refs.business) {
                            this.showSpin();
                            this.$refs.business.formValidate("submitDataFn");
                        }
                    }
                });
            },

            // 审核等按钮
            approve(name) {
                if (!name) return;
                let t, val;
                switch (name) {
                    case "pass":
                        t = "审核通过";
                        val = "suggestCheckF";
                        break;
                    case "return":
                        t = "退件";
                        val = "sendBack";
                        break;
                    case "reject":
                        t = "拒绝";
                        val = "suggestRejectF";
                        break;
                }
                this.$Modal.confirm({
                    title: `确认${t}？`,
                    onOk: () => {
                        if (this.$refs.business) {
                            this.showSpin();
                            this.approveType = name;
                            this.approvalOperating = val;
                            // 验证 business 组件数据 给下一个方法
                            this.$refs.business.formValidate("childBusinessForm");
                        }
                    }
                });
            },
            change(status) {
                this.$Message.info(`Status: ${status}`);
            },
            // 保存 请求
            saveDataFn(data) {
                if (!data) return;
                data.selecteds = this.$refs.business.selecteds
                console.log("this.$refs.business.selecteds",this.$refs.business.selecteds)
                data.channelFullName = this.$refs.contractInfo.formData.channelFullName
                data.channelBelong = this.$refs.contractInfo.formData.channelBelong;
                data.businessSource = businessSource(this.roleType);
                console.log("data520",data)
                let d = this.formatData(data);
                saveContractReissue(d)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            if (this.pageInfo.caseNo) this.pageInfo = res.data;

                            this.$refs.business.formData = res.data;
                            console.log(res.data);
                            this.$Message.success("保存成功!");
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            },
            //提交 请求
            submitDataFn(data) {
                if (!data) return;
                let d = this.formatData(data);
                this.formData();
                submitContractReissue(this.contractForm)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            this.$Message.success("提交成功!");
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            },
            // 审批 请求
            // Business 组件数据
            childBusinessForm(data) {
                if (!data) return;
                let bussiness = this.formatData(data);
                // 验证 contractCurrentUser 组件数据 传给下个方法
                this.$refs.contractCurrentUser.formValidate("childUserForm", bussiness,this.approveType);
                console.log("bussiness", bussiness)
            },
            // contractCurrentUser 组件数据
            childUserForm(data) {
                if (!data) return;
                data.selecteds = this.$refs.business.selecteds
                let d = Object.assign({}, data, {
                    approvalOperating: this.approvalOperating
                });
                console.log("d", d)
                saveProposalApplicationInfo(d)
                    .then(res => {
                        if (res.code === "0000") {
                            this.$Message.success(res.msg);
                            if (res.data) {
                                if (this.paymentStatus === 'suspensionPayment') {
                                    this.updatePaymentStatus({
                                        contractNo: this.contractNo,
                                        paymentStatus: 'waitPayment'
                                    });
                                }
                            }
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            },

            // 格式化
            formatData(data) {
                if (!data) return {};
                let d = Object.assign({}, this.pageInfo, data);
                // if (d.createTime instanceof Date)
                //     d.createTime = moment(d.createTime).format("YYYY-MM-DD HH:mm:ss");
                // if (d.endDate instanceof Date)
                //     d.endDate = moment(d.endDate).format("YYYY-MM-DD HH:mm:ss");
                return d;
            },
            showSpin() {
                this.$emit("showSpin");
            },
            closeSpin() {
                this.$emit("closeSpin");
            },
            back() {
                this.$emit("back");
            }
        }
    };
</script>
